package cn.myweixin.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.authenticator.SingleSignOnSessionKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cn.myweixin.po.Notice;
import cn.myweixin.po.SignKey;
import cn.myweixin.po.Trainee;
import cn.myweixin.service.impl.NoticeImpl;
import cn.myweixin.service.impl.SignImpl;
import cn.myweixin.service.impl.TraineeImpl;

@Controller
@RequestMapping("/sign")
public class SignController {
	@Autowired
	 private TraineeImpl traineeImpl;
	@Autowired
	private NoticeImpl noticeImpl;
	
	@Autowired
	private SignImpl signImpl;
	
	@RequestMapping("/querysigns")
	public ModelAndView  querysigns(HttpServletRequest request) throws Exception{
		
		Cookie[] cookies=request.getCookies();
		if(cookies!=null){
		for (Cookie cookie : cookies) {
			if(cookie.getName().equals("vx_openid")){
				    String id=cookie.getValue();
				    Trainee trainee= traineeImpl.selectByPrimaryKey(id);
				    ModelAndView modelAndView=new ModelAndView();
				    if(trainee!=null){
				
				    	List<Notice> noticesExpect=noticeImpl.findListExpect(id);//查找用户未报名的通过
				    	List<Notice> noticesExists=noticeImpl.findListExists(id);//查找用户已报名的通过
				    	modelAndView.addObject("noticesExpect", noticesExpect);
				    	modelAndView.addObject("noticesExists", noticesExists);
				    	modelAndView.setViewName("notices/sign");
				    	return modelAndView;
				    }
				    else{
				    	
				    	return new ModelAndView(new RedirectView("user/register.action"));
				    	
				    }
				}
			}
		}
		
		return new ModelAndView(new RedirectView("user/cookievalidate.action"));
		
	}
	
	@RequestMapping(value="/insertsign",method={RequestMethod.POST,RequestMethod.GET})
	public void   insertsign(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="id",required=true) Integer notice_id) throws Exception{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		String trainid=(String) session.getAttribute("openid");
		SignKey signKey=new SignKey();
	    signKey.setTraineeId(trainid);
		signKey.setNoticeId(notice_id);	
		PrintWriter out=response.getWriter();
		if(signImpl.insert(signKey)==1){
			out.print("报名成功!");
		}
		else
			out.print("报名失败!");
		out.flush();
		out.close();
		
	}
	
	
	

	@RequestMapping(value="/deletesign",method={RequestMethod.POST,RequestMethod.GET})
	public void   deletesign(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="id",required=true) Integer notice_id) throws Exception{
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		String trainid=(String) session.getAttribute("openid");
		SignKey signKey=new SignKey();
		signKey.setTraineeId(trainid);
		signKey.setNoticeId(notice_id);	
		PrintWriter out=response.getWriter();
		if(signImpl.deleteByPrimaryKey(signKey)==1){
			out.print("取消成功!");
		}
		else
			out.print("取消失败!");
		out.flush();
		out.close();
		
	}
	
}
